import { Component, Input, OnInit } from '@angular/core';
import { deepClone, isObject } from '../../../utils/utils';
import { ApesMessageService } from '@shared/apes/message';
import { ReuseTabService } from '@shared/apes/reuse-tab';
import { ActivatedRoute, Router } from '@angular/router';
import { MenuService } from '@core';
import { Subscription } from 'rxjs';
import { ButtonService } from '@core/services/button.service';

@Component({
  selector: 'apes-relevancy-link',
  template: `
    <div class="apes-relevancy-link" *ngIf="_relevancy && _relevancy['links'] && _relevancy['links'].length != 0 ">
      <div class="apes-relevancy-links">
        <ng-container *ngFor="let param of _relevancy['links']">
          <div class="apes-relevancy-link-btn" [ngClass]="{'apes-relevancy-link-btn-current': showCurUrl(param.link)}"
               (click)="openLink(param.link)">{{param.link.title}}</div>
        </ng-container>
      </div>
    </div>
  `,
})

export class ApesRelevancyLinkComponent implements OnInit {

  @Input() set relevancy(value) {
    if (value) {
      this._relevancy = this.filtrationRelevancy(value);
    }
  }

  get relevancy() {
    return this._relevancy;
  }

  private change$: Subscription;

  _relevancy: any;
  menuList: any = [];

  constructor(
    private msg: ApesMessageService,
    protected rs: ReuseTabService,
    private router: Router,
    protected reuseTabService: ReuseTabService,
    protected route: ActivatedRoute,
    protected menuService: MenuService,
    private buttonService: ButtonService,
  ) {
    this.change$ = <any>this.menuService.change.subscribe(res => {
      let menuList = [];
      this.filtrationUrl(res, menuList);
      this.menuList = deepClone(menuList);
    });
  }

  ngOnInit(): void {
  }

  openLink(param) {

    let formId = param.id;

    let sendData = {};

    let config = {};

    this.openContextLink(sendData, formId, config);
  }

  openContextLink(data: any, method: string, params?: object) {
    if (isObject(params)) Object.assign(data, params);
    if (method == null) {
      this.msg.error('该链接尚未存在，麻烦核实！', { 'apesDuration': 5000 });
      return false;
    }

    const formId = method;
    const url = '/' + formId;
    if (this.rs.exists(url) || this.rs.curUrl === url) {
      this.router.navigateByUrl(formId);
    } else {
      this.router.navigate([formId]);
    }

    this.buttonService.getMenuLog(url, 'assembly');
  }

  showCurUrl(param) {
    let formId = param.id;
    let url = '/' + formId;
    let curURL = this.reuseTabService.getUrl(this.route.snapshot);
    return curURL == url;
  }

  filtrationUrl(params, list: any[]) {
    params.forEach((param) => {
      if (param.children.length != 0) {
        this.filtrationUrl(param.children, list);
      } else {
        if (param.link != '') {
          list.push(param.link);
        }
      }
    });
  }

  filtrationRelevancy(value) {
    let links = value['links'];
    let list = [];
    links.forEach((link) => {
      let id = link.link.id;
      this.menuList.forEach((menu) => {
        let m = menu.charAt(0) === '/' ? menu.substring(1) : menu;
        if (m == id) {
          list.push(link);
        }
      });
    });
    return { links: list };
  }
}
